package com.zhuss.team.processor;

import com.zhuss.bean.bo.team.information.UpdateInfoBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.team.db.dao.ext.InformationDao;
import com.zhuss.team.db.dao.ext.InformationSonDao;
import com.zhuss.team.db.entity.Information;
import com.zhuss.team.db.entity.InformationSon;
import com.zhuss.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: zhushanshuo
 * @Date: 2020/3/3 14:40
 */
@Component("informationUpdate")
public class InformationUpdate extends AbstractProcessor<Object, UpdateInfoBo> implements Processor<Object, UpdateInfoBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
    private InformationSonDao informationSonDao;

    @Override
    public Object process(UpdateInfoBo bo) throws WpsException {
        Information info = new Information();
        info.setInfoId(bo.getInfoId());
        info.setType(bo.getType());
        info.setPerson(bo.getPerson());
        info.setTitle(bo.getTitle());
        info.setVisible(bo.getVisible());
        info.setContent(bo.getContent());
        info.setFormat(bo.getFormat());
        info.setArea(bo.getArea());
        info.setInfoKey(bo.getInfoKey());

        info.setModifier(bo.getConcurrentUserName());
        info.setModifierId(bo.getConcurrentUserId());
        info.setModifyTime(new Date());
        informationDao.updateByPrimaryKeySelective(info);

        InformationSon infoSon = new InformationSon();
        infoSon.setInfoId(info.getInfoId());
        infoSon.setInfoTitle(info.getTitle());

        infoSon.setModifier(bo.getConcurrentUserName());
        infoSon.setModifierId(bo.getConcurrentUserId());
        infoSon.setModifyTime(new Date());
        informationSonDao.updateByPrimaryKeySelective(infoSon);
        return null;
    }

    @Override
    public Object log(UpdateInfoBo param) throws WpsException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.协同发布.key());
        record.setAction(LogActionEnum.信息发布修改.key());
        record.setTargetId(param.getInfoId());
        record.setTargetName(param.getTitle());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }

    @Override
    public void check(UpdateInfoBo updateInfoBo) throws WpsException {

    }
}
